﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.Core;

namespace Utilities
{
    public class GlobalInit
    {
        public static void loadValueForGlobalServerDetail()
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(Utilities.GlobalFile.SERVERS_FILENAME);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode(Utilities.GlobalRunning.strSERVER);
            xmlGetter.setLine1Node("variable");

            DataTable dtTarget = xmlGetter.getLine1NodeValues();
            foreach (DataRow dr in dtTarget.Rows)
            {
                string strKey = dr[0].ToString();
                string strValue = dr[1].ToString();
                switch (strKey)
                {
                    case "SERVER_URL":
                        Utilities.GlobalRunning.SERVER_URL = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded SERVER_URL = [" + Utilities.GlobalRunning.SERVER_URL + "]");
                        break;
                    case "SERVER_USER":
                        Utilities.GlobalRunning.SERVER_USER = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded SERVER_USER = [" + Utilities.GlobalRunning.SERVER_USER + "]");
                        break;
                    case "SERVER_PASSWORD":
                        Utilities.GlobalRunning.SERVER_PASSWORD = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded SERVER_PASSWORD = [" + Utilities.GlobalRunning.SERVER_PASSWORD + "]");
                        break;
                    default:
                        break;
                }
            }

            Utilities.GlobalObject.LOGGER.log("---------------READY TO GO----------------");
        }

        public static void loadValueForGlobalConfig(string strPathConfigFile)
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(strPathConfigFile);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode("global");
            xmlGetter.setLine1Node("config");
            xmlGetter.setLine2Node("variable");

            DataTable dtTarget = xmlGetter.getLine2NodeValues();
            foreach (DataRow dr in dtTarget.Rows)
            {
                string strKey = dr[0].ToString();
                string strValue = dr[1].ToString();
                switch (strKey)
                {
                    case "STR_EMAIL_SERVER":
                        Utilities.GlobalConfig.STR_EMAIL_SERVER = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded STR_EMAIL_SERVER = [" + Utilities.GlobalConfig.STR_EMAIL_SERVER + "]");
                        break;
                    case "STR_EMAIL_SUBJECT":
                        Utilities.GlobalConfig.STR_EMAIL_SUBJECT = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded STR_EMAIL_SUBJECT = [" + Utilities.GlobalConfig.STR_EMAIL_SUBJECT + "]");
                        break;
                    case "STR_EMAIL_RECIPIENT":
                        Utilities.GlobalConfig.STR_EMAIL_RECIPIENT = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded STR_EMAIL_RECIPIENT = [" + Utilities.GlobalConfig.STR_EMAIL_RECIPIENT + "]");
                        break;
                    case "STR_EMAIL_SENDER":
                        Utilities.GlobalConfig.STR_EMAIL_SENDER = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded STR_EMAIL_SENDER = [" + Utilities.GlobalConfig.STR_EMAIL_SENDER + "]");
                        break;
                    case "STR_SYSTEM_NAME":
                        Utilities.GlobalConfig.STR_SYSTEM_NAME = strValue;
                        Utilities.GlobalObject.LOGGER.log("Loaded STR_SYSTEM_NAME = [" + Utilities.GlobalConfig.STR_SYSTEM_NAME + "]");
                        break;
                    case "INT_WAIT_INTERVAL_MILISECOND":
                        Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND = int.Parse(strValue);
                        Utilities.GlobalObject.LOGGER.log("Loaded INT_WAIT_INTERVAL_MILISECOND = [" + Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND + "]");
                        break;
                    case "INT_WAIT_TIMEOUT_MILISECOND":
                        Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND = int.Parse(strValue);
                        Utilities.GlobalObject.LOGGER.log("Loaded INT_WAIT_TIMEOUT_MILISECOND = [" + Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND + "]");
                        break;
                    default:
                        break;
                }
            }
        }

        public static void loadValueForGlobalFile(string strPathConfigFile)
        {
            XMLGetter xmlGetter = new XMLGetter();
            xmlGetter.setXmlFilePath(strPathConfigFile);
            xmlGetter.setMasterNode("SM2");
            xmlGetter.setHeaderNode("global");
            xmlGetter.setLine1Node("file");
            xmlGetter.setLine2Node("variable");

            DataTable dtTarget = xmlGetter.getLine2NodeValues();
            foreach (DataRow dr in dtTarget.Rows)
            {
                string strKey = dr[0].ToString();
                string strValue = dr[1].ToString();
                switch (strKey)
                {
                    case "MAPPING_FILENAME":
                        Utilities.GlobalFile.MAPPING_FILENAME = strValue;
                        break;
                    case "LOG_FILENAME":
                        Utilities.GlobalFile.LOG_FILENAME = strValue;
                        break;
                    case "REPORT_FILENAME":
                        Utilities.GlobalFile.REPORT_FILENAME = strValue;
                        break;
                    case "REPORT_TEMPLATE_FILENAME":
                        Utilities.GlobalFile.REPORT_TEMPLATE_FILENAME = strValue;
                        break;
                    case "SCHEDULER_FILENAME":
                        Utilities.GlobalFile.SCHEDULER_FILENAME = strValue;
                        break;
                    case "SERVERS_FILENAME":
                        Utilities.GlobalFile.SERVERS_FILENAME = strValue;
                        break;
                    case "SERVER_HARDATA_FILENAME":
                        Utilities.GlobalFile.SERVER_HARDATA_FILENAME = strValue;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
